/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package helper;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Le Quynh Nhu
 */
public class sessionTool {

    public static void removeSession(String sessionName) {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(sessionName);
    }

    public static void setSession(String sessionName, Object sessionObject) {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(sessionName, sessionObject);
    }

    public static Object getSession(String sessionName) {
        return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(sessionName);
    }

    public static void removeAllSession() {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().clear();
    }

    public static String getRequestParameter(String key) {
        return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(key);
    }

    public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {
        int size = selectOne ? entities.size() + 1 : entities.size();
        SelectItem[] items = new SelectItem[size];
        int i = 0;
        if (selectOne) {
            items[0] = new SelectItem("", "-----Select----");
            i++;
        }
        for (Object x : entities) {
            items[i++] = new SelectItem(x, x.toString());
        }
        return items;
    }

    public static List<SelectItem> getSelectItems(List data) {
        List<SelectItem> items = new LinkedList<SelectItem>();
        for (Object obj : data) {
            SelectItem item = new SelectItem(obj);
            items.add(item);
        }
        return items;
    }

    public static List<Object> toList(Object array) {
        if (array instanceof Object[]) {
            return Arrays.asList((Object[]) array);
        }
        int size = Array.getLength(array);
        ArrayList<Object> list = new ArrayList<Object>(size);
        for (int i = 0; i < size; i++) {
            list.add(Array.get(array, i));
        }
        return list;
    }

    public static List<?> toListIter(Iterator<?> iter) {
        List<Object> list = new ArrayList<Object>();
        while (iter.hasNext()) {
            list.add(iter.next());
        }
        return list;
    }

    public static FacesContext getFacesContext() {

        return FacesContext.getCurrentInstance();

    }

    /**

     * Method to create a redirect URL. The assumption is that the JSF servlet mapping is

     * "faces", which is the default

     *

     * @param view the JSP  page to redirect to

     * @return a URL to redirect to

     */
    public static String getPageURL(String view) {

        FacesContext facesContext = getFacesContext();

        ExternalContext externalContext = facesContext.getExternalContext();

        String url =
                ((HttpServletRequest) externalContext.getRequest()).getRequestURL().toString();

        StringBuffer newUrlBuffer = new StringBuffer();

        newUrlBuffer.append(url.substring(0, url.lastIndexOf("faces/")));

        newUrlBuffer.append("faces");

        String targetPageUrl = view.startsWith("/") ? view : "/" + view;

        newUrlBuffer.append(targetPageUrl);

        return newUrlBuffer.toString();

    }

    /**

     * Get an HTTP Request attribute.

     * @param name attribute name

     * @return attribute value

     */
    public static Object getRequestAttribute(String name) {

        return getFacesContext().getExternalContext().getRequestMap().get(name);

    }

    /**

     * Set an HTTP Request attribute.

     * @param name attribute name

     * @param value attribute value

     */
    public static void setRequestAttribute(String name, Object value) {

        getFacesContext().getExternalContext().getRequestMap().put(name,
                value);

    }

    public static String getStringRealPath(String tenFolder) {
        FacesContext con = FacesContext.getCurrentInstance();
        HttpSession s = (HttpSession) con.getExternalContext().getSession(false);
        ServletContext sc = s.getServletContext();
        String path = sc.getRealPath(tenFolder);
        int p = 0;
        while (p != -1) {
            p = path.indexOf("\\", p);
            if (p >= 0) {
                path = path.substring(0, p) + "/" + path.substring(p + 1);
                p += 1;
            }
        }

        return path;
    }

    public static void redirectPage(String szPage) {
        FacesContext context = FacesContext.getCurrentInstance();
        javax.faces.application.Application app = context.getApplication();
        UIViewRoot view = app.getViewHandler().createView(context, szPage);
        context.setViewRoot(view);
        context.renderResponse();
    }

    public static HttpServletResponse getResponse() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
        return response;
    }

 
    public static void printData(String data) {
        System.out.println(data);
    }
}
